<?php

namespace Xin\LaravelFortify\Plugin;

use Nwidart\Modules\Facades\Module;
use Xin\Support\Fluent;

class Plugin
{
    /**
     * @var array
     */
    protected static $pluginInfos = [];

    /**
     * 获取插件信息
     * @param string $pluginName
     * @return \Nwidart\Modules\Module
     */
    public static function info(string $pluginName)
    {
        if (!isset(self::$pluginInfos[$pluginName])) {
            self::$pluginInfos[$pluginName] = Module::find($pluginName);
        }

        return self::$pluginInfos[$pluginName];
    }

    /**
     * 判断插件是否存在
     * @param string $pluginName
     * @return bool
     */
    public static function has(string $pluginName)
    {
        return Module::has($pluginName);
    }

    /**
     * 获取插件配置信息
     * @param string $pluginName
     * @return array
     */
    public static function manifest(string $pluginName, string $key = null, $default = null)
    {
        return $key ? new Fluent(self::info($pluginName)->json())
            : self::info($pluginName)->get($key, $default);
    }

    /**
     * 获取插件根目录
     * @param string $path
     * @return string
     */
    public static function rootPath(string $path = '')
    {
        $rootPath = config('modules.paths.modules');
        return join_paths($rootPath, $path);
    }

    /**
     * 获取插件目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function basePath(string $pluginName, string $path = '')
    {
        return self::rootPath(join_paths($pluginName, $path));
    }

    /**
     * 获取插件应用目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function appPath(string $pluginName, string $path = '')
    {
        return self::basePath($pluginName, join_paths('app', $path));
    }


    /**
     * 获取插件应用Admin目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function adminPath(string $pluginName, string $path = '')
    {
        return self::appPath($pluginName, join_paths('Admin', $path));
    }

    /**
     * 获取插件应用Admin Controllers目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function adminControllerPath(string $pluginName, string $path = '')
    {
        return self::adminPath($pluginName, join_paths('Controllers', $path));
    }

    /**
     * 获取插件应用Admin Requests目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function adminRequestPath(string $pluginName, string $path = '')
    {
        return self::adminPath($pluginName, join_paths('Requests', $path));
    }

    /**
     * 获取插件应用API目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function apiPath(string $pluginName, string $path = '')
    {
        return self::appPath($pluginName, join_paths('Http', $path));
    }

    /**
     * 获取插件应用Api Controllers目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function apiControllerPath(string $pluginName, string $path = '')
    {
        return self::apiPath($pluginName, join_paths('Controllers', $path));
    }

    /**
     * 获取插件应用Api Controllers目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function apiRequestPath(string $pluginName, string $path = '')
    {
        return self::apiPath($pluginName, join_paths('Requests', $path));
    }

    /**
     * 获取插件配置目录
     * @param string $pluginName
     * @param string $path
     * @return string
     */
    public static function configPath(string $pluginName, string $path = '')
    {
        return self::basePath($pluginName, join_paths('config', $path));
    }
}
